unit ReportForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, ExtCtrls, ShellAPI;

type
  TRep = class(TForm)
    StringGrid: TStringGrid;
    Panel1: TPanel;
    btRepair: TButton;
    btClose: TButton;
    btCSV: TButton;
    btScript: TButton;
    procedure btRepairClick(Sender: TObject);
    procedure btCloseClick(Sender: TObject);
    procedure btCSVClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btScriptClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure InitReport;
    procedure AddToReport(Tip, ObjectName, Msg : String);
  end;

var
  Rep: TRep;

implementation

{$R *.DFM}

{ TRepForm }

{ TRep }

procedure TRep.FormCreate(Sender: TObject);
begin
   StringGrid.Cells[0, 0] := 'Type';
   StringGrid.Cells[1, 0] := 'Object Name';
   StringGrid.Cells[2, 0] := 'Message';
end;

procedure TRep.InitReport;
var
   i: Integer;
begin
   StringGrid.RowCount := 2;
   for i := 0 to StringGrid.ColCount-1 do
      StringGrid.Cells[i, 1] := '';
end;

procedure TRep.AddToReport(Tip, ObjectName, Msg: String);
var
   Poz: Integer;
begin
   With StringGrid do
   begin
      Poz := RowCount;
      if Cells[0, Poz-1] <> '' then
         Inc(Poz);
      Cells[0, Poz-1] := Tip;
      Cells[1, Poz-1] := ObjectName;
      Cells[2, Poz-1] := Msg;
      RowCount := Poz;
   end;
end;

procedure TRep.btRepairClick(Sender: TObject);
begin
   ModalResult := mrOK;
end;

procedure TRep.btCloseClick(Sender: TObject);
begin
   ModalResult := mrCancel;
end;

procedure TRep.btCSVClick(Sender: TObject);
var
   i: Integer;
   CSVList: TStringList;
   LS: Char;
begin
   LS := ListSeparator;
   CSVList := TStringList.Create;
   CSVList.Clear;
   for i := 1 to StringGrid.RowCount-1 do
      CSVList.Add (StringGrid.Cells[0, i] + LS + StringGrid.Cells[1, i] + LS + StringGrid.Cells[2, i] + LS);
   CSVList.SaveToFile(ExtractFilePath(Application.ExeName) + 'Report.CSV');
   CSVList.Free;
end;

procedure TRep.btScriptClick(Sender: TObject);
begin
  ShellExecute(handle,
             NIL,
             'Notepad.exe',
             PChar(ExtractFilePath(ParamStr(0))+'ibrest.sql'),
             NIL,
             SW_SHOWNORMAL);
end;

end.
